using DapperExtensionsNew.Data;
using DapperExtensionsNew.Utils;
using System.Text;

namespace DapperExtensionsNew.Configuration.AnalyzerOptions
{
	internal class MySqlDatabaseOptions : SqlDatabaseOptions
	{
		public MySqlDatabaseOptions()
		{
			base.StartDelimiter = (base.EndDelimiter = "`");
			base.Encapsulation = "`{0}`";
			base.IdentitySql = "SELECT LAST_INSERT_ID() AS id";
			base.FromSql = " from {0} ";
			base.PagedListSql = "Select {SelectColumns} from {TableName} {WhereClause} {IsDeleted} Order By {OrderBy} LIMIT {Offset},{RowsPerPage}";
			base.PagedListCountSql = "SELECT COUNT(1) FROM {TableName} {WhereClause} {IsDeleted}";
		}

		public override string GetOperationPatten(SqlOperation operation)
		{
			switch (operation)
			{
			case SqlOperation.Insert:
				return "INSERT INTO `{TableName}` ({Columns}) VALUES ({Values}); SELECT @@IDENTITY AS `Id` ";
			case SqlOperation.Update:
				return "UPDATE `{TableName}` SET {SetColumns} {WhereClause}";
			case SqlOperation.Delete:
				return "DELETE FROM `{TableName}` {WhereClause}";
			default:
				return null;
			}
		}

		public override string GetSelectSql(string columns, string tableName)
		{
			StringBuilder stringBuilder = StringBuilderCache.Acquire();
			stringBuilder.Append("SELECT ");
			stringBuilder.Append(columns);
			stringBuilder.Append(" FROM `");
			stringBuilder.Append(tableName);
			stringBuilder.Append("`");
			return StringBuilderCache.GetStringAndRelease(stringBuilder);
		}

		public override string GetPageSql(int skip, int take)
		{
			return $" limit {skip},{take}";
		}
	}
}
